﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Cfg;
using FluentNHibernate.Automapping;
using ProjectManagement.Domain.Entities;
using NHibernate.Tool.hbm2ddl;
using NHibernate;
using NHibernate.Cfg;

namespace ProjectManagement.Domain.AutoMappings
{
	public class NHibernateHelper
	{
		public static ISessionFactory CreateSessionFactory()
		{
			var cfg = new ProjectManagementSystemConfiguration();
			var configuration = Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
				.ConnectionString(c => c.FromConnectionStringWithKey("PermissionManage")))
				.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<User>(cfg)).ExportTo("c:\\path"))//.ExportTo("c:\\path"))导出映射文件到C
				.ExposeConfiguration(CreateSchema)
				.BuildConfiguration();

			return configuration.BuildSessionFactory();
			////var exporter = new SchemaExport(configuration);
			////exporter.SetOutputFile(@"c:\abc.sql");
			////exporter.Execute(true, false, false);
		}

		private static void CreateSchema(Configuration cfg)
		{
			var schemaExport = new SchemaExport(cfg);
			schemaExport.SetOutputFile(@"c:\abc.sql");
			schemaExport.Create(false, false);
		}
	}
}
